﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UnisysMassiveImporter_v2.Entidades.Configuracion;

namespace Configurator
{
    public partial class frm_trehad : Form
    {
        Frm_Main frm = new Frm_Main();
        Thread_Config thread = new Thread_Config();
        string threadPath;
        public frm_trehad( string h,string path)
        {
            InitializeComponent();
            cargarThread(Thread_Config.Load(h));            
            threadPath = path;
        }

        public frm_trehad(string path) {
            InitializeComponent();            
            threadPath = path;
        }

        public void cargarThread(Thread_Config th) {
            txt_Campos.Text = th.required_fields;
            txt_DataPath.Text = th.DataPath;
            txt_Delay.Text = Convert.ToString(th.Delay);
            txt_Dominio.Text = th.IIFDomain;
            txt_ImagesPath.Text = th.ImagesPath;
            txt_InputQueue.Text = th.InputQueue;
            txt_MaxFiles.Text = Convert.ToString(th.MaxFiles);
            txt_Nombre.Text = th.Name;
            txt_Password.Text = th.IIFPassword;
            txt_Usuario.Text = th.IIFUser;
            
            switch (th.DirectoryOrganization)
            {
                case 1:
                    cbx_DirectoryOrganization.SelectedIndex = 0;

                    break;
                    
                case 2:
                    cbx_DirectoryOrganization.SelectedIndex = 1;
                    break;

                case 3:
                    cbx_DirectoryOrganization.SelectedIndex = 2;
                    break;
            }

            switch (th.Filetype)
            {
                case 1:
                    cbx_FileType.SelectedIndex = 0;
                    break;

                case 2:
                    cbx_FileType.SelectedIndex = 1;
                    break;

                case 3:
                    cbx_FileType.SelectedIndex = 2;
                    break;
            }
            
        }
        private void btn_FindData_Click(object sender, EventArgs e)
        {
             fbd_browse.SelectedPath = "C:\\";


            DialogResult result = fbd_browse.ShowDialog();
            if (result.ToString() == "OK")
            {
                txt_DataPath.Text = fbd_browse.SelectedPath;
            }
        }

        private void btn_FindImages_Click(object sender, EventArgs e)
        {
            fbd_browse.SelectedPath = "C:\\";


            DialogResult result = fbd_browse.ShowDialog();
            if (result.ToString() == "OK")
            {
                txt_ImagesPath.Text = fbd_browse.SelectedPath;
            }

        }

        private void btn_Cancelar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_Guardar_Click(object sender, EventArgs e)
        {
            try
            {
               
               
                
                thread.DataPath = txt_DataPath.Text;
                thread.ImagesPath = txt_ImagesPath.Text;
                thread.Delay = Convert.ToInt32(txt_Delay.Text);

               
                switch (cbx_DirectoryOrganization.SelectedIndex)
                {
                    case 0:
                        thread.DirectoryOrganization = 1;
                        break;

                    case 1:
                        thread.DirectoryOrganization = 2;
                        break;

                    case 2:
                        thread.DirectoryOrganization = 3;
                        break;


                }
                switch(cbx_FileType.SelectedIndex)
                {
                    case 0:
                        thread.Filetype = 1 ;
                        break;

                    case 1:
                        thread.Filetype = 2;
                        break;

                    case 2:
                        thread.Filetype = 3;
                        break;
                }
                
                thread.IIFDomain = txt_Dominio.Text;
                thread.IIFPassword = a.b(txt_Password.Text);
                thread.IIFUser = txt_Usuario.Text;
                thread.InputQueue = txt_InputQueue.Text;
                thread.MaxFiles = Convert.ToInt32(txt_MaxFiles.Text);
                thread.Name = txt_Nombre.Text;
                thread.required_fields = txt_Campos.Text;
                
                thread.Salvar(threadPath, thread);

                MessageBox.Show(null, "La configuración  del Thread ha sido guardada exitosamente.", "Unisys Massive Importer Configurator", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (Exception exc) {
                MessageBox.Show("Ocurrio un error:"+ exc);
            }

        }

        private void btn_FindCampos_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = "C:\\";
            


            DialogResult result = openFileDialog1.ShowDialog();
            if (result.ToString() == "OK")
            {
                txt_Campos.Text = openFileDialog1.FileName;
            }

        }

        private void cbx_DirectoryOrganization_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cbx_DirectoryOrganization.SelectedIndex)
            {
                case 0:
                    ptb_Directory.Image = Configurator.Properties.Resources.Directory;
                    break;

                case 1:
                    ptb_Directory.Image = Configurator.Properties.Resources.Directory2;
                    break;

                case 2:
                    ptb_Directory.Image = Configurator.Properties.Resources.Directory3;
                    break;


            }
        }

        

       

        

        
    }
}
